package com.blog4j.user.component;

import com.blog4j.api.client.FeignServer;
import com.blog4j.api.vo.MessageVo;
import com.blog4j.api.vo.NoticeEmailVo;
import com.blog4j.common.enums.MessageStatusEnum;
import com.blog4j.common.enums.YesOrNoEnum;
import com.blog4j.common.model.LogVo;
import com.blog4j.common.utils.CommonUtil;
import com.blog4j.user.entity.MessageEntity;
import com.blog4j.user.mapper.MessageMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/8/10 00:00
 **/
@Component
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Slf4j
public class AsyncService {
    private final FeignServer feignServer;
    private final MessageMapper messageMapper;

    /**
     * 用户申请加入组织，给组织管理员发送审批邮件
     *
     * @param toEmail 组织管理员邮箱地址
     * @param userName 申请用户的用户名称
     */
    @Async("customExecutor")
    public void sendEmailToOrganizationAdmin(String toEmail, String userName) {
        if (StringUtils.isBlank(toEmail) || StringUtils.isBlank(userName)) {
            return;
        }
        log.info("给组织管理员发送邮件，用户申请加入组织！");
        NoticeEmailVo emailVo = NoticeEmailVo.builder()
                .toEmail(toEmail)
                .acceptUserName(userName)
                .build();
        feignServer.sendSystemEmailToOrganizationAdmin(emailVo);
    }

    /**
     * 用户申请加入组织，审批后，给用户发送邮件通知
     *
     * @param toEmail 用户邮箱地址
     * @param organizationName 组织名称
     */
    @Async("customExecutor")
    public void sendEmailToUser(String toEmail, String organizationName) {
        if (StringUtils.isBlank(toEmail) || StringUtils.isBlank(organizationName)) {
            return;
        }
        log.info("用户申请加入组织，审批后，给用户发送邮件通知！");
        feignServer.sendEmailToUser(toEmail, organizationName);
    }

    /**
     * 给超级管理员发送审批组织的邮件
     *
     * @param toEmail 组织管理员邮箱地址
     * @param userName 申请用户的用户名称
     */
    @Async("customExecutor")
    public void sendEmailToSuperAdmin(String toEmail, String userName) {
        if (StringUtils.isBlank(toEmail) || StringUtils.isBlank(userName)) {
            return;
        }
        log.info("给组织管理员发送邮件，用户申请加入组织！");
        NoticeEmailVo emailVo = NoticeEmailVo.builder()
                .toEmail(toEmail)
                .acceptUserName(userName)
                .build();
        feignServer.sendSystemEmailToSuperAdmin(emailVo);
    }

    /**
     * 用户申请创建组织 审批后 给用户发送邮件
     *
     * @param toEmail 用户邮箱地址
     * @param organizationName 组织名称
     */
    @Async("customExecutor")
    public void sendEmailToUserForApproveOrganization(String toEmail, String organizationName) {
        if (StringUtils.isBlank(toEmail) || StringUtils.isBlank(organizationName)) {
            return;
        }
        log.info("用户申请创建组织 审批后 给用户发送邮件！");
        feignServer.sendEmailToUserForApproveOrganization(toEmail, organizationName);
    }

    /**
     * 用户注册成功，给用户发消息
     *
     * @param messageVo 消息
     */
    @Async("customExecutor")
    public void sendMessage(MessageVo messageVo) {
        MessageEntity message = MessageEntity.builder()
                .messageContent(messageVo.getMessageContent())
                .toUserId(messageVo.getToUserId())
                .fromUserId(messageVo.getFromUserId())
                .messageStatus(MessageStatusEnum.UNREAD.getCode())
                .deleted(YesOrNoEnum.NO.getCode())
                .createTime(CommonUtil.getCurrentDateTime())
                .build();
        messageMapper.insert(message);
    }

    @Async("customExecutor")
    public void addLog(LogVo logVo) {
        feignServer.addLog(logVo);
    }
}
